package Java8实战._1_Lambda._9_Lambda应用;

import Java8实战._1_Lambda.Apple;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class _9_2_Lambda应用_Comparator {

    public static void main(String[] args) {

        List<Apple> inventory = Arrays.asList(
                new Apple("red",150),
                new Apple("green",123),
                new Apple("green",189)
        );

        List<Apple> result;

        //1.传递代码
        inventory.sort(new AppleComparator());

        //2.匿名类
        inventory.sort(new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getWeight().compareTo(o2.getWeight());
            }
        });

        //3.1.Lambda
        inventory.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));

        //3.2.Lambda
        inventory.sort((a1, a2) -> a1.getWeight().compareTo(a2.getWeight()));

        //3.3.Lambda
        inventory.sort(Comparator.comparing((a) -> a.getWeight()));

        //4.方法引用
        inventory.sort(Comparator.comparing(Apple::getWeight));
    }

    public static class AppleComparator implements Comparator<Apple> {

        @Override
        public int compare(Apple o1, Apple o2) {
            return o1.getWeight().compareTo(o2.getWeight());
        }
    }


}
